{% extends '_base_create_update.html' %}

{% block top_css %}
    {{ super() }}

    <!--Treeview-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='plugins/bootstrap-treeview/css/bootstrap-treeview.min.css') }}">
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">

{% endblock %}


<!-- Content head -->
{% block head_title %}
    角色管理
{% endblock %}

{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">权限管理</a></li>
        <li class="active">角色管理</li>
    </ol>
{% endblock %}

<!-- Content body -->
{% block body_title %}
    修改角色信息
{% endblock %}

{% block body_form %}
    <form name="roleForm" action="" class="form-horizontal">
        <div class="panel-body">
            <p class="bord-btm pad-ver text-main text-bold">角色信息</p>

            <div class="row">
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>角色名称</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="name" value="{{ name }}" placeholder="请输入角色名称" data-rule="角色名称: required;" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label">角色描述</label>
                            <div class="col-lg-8">
                                <textarea class="form-control" name="describe" placeholder="请填写角色描述信息">{{ describe }}</textarea>
                            </div>
                        </div>
                    </fieldset>
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label">角色权限</label>
                            <div class="col-lg-8">
                                <div class="role-wrap">
                                    <div class="role-tree"></div>
                                </div>
                            </div>
                        </div>
                    </fieldset>

                </div>
            </div>
        </div>

        <div class="panel-footer">
            <div class="row">
                <div class="col-sm-6 col-sm-offset-6">
                    <div class="col-sm-7 col-sm-offset-3">
                        <button class="btn btn-mint pull-right" type="submit">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
{% endblock %}



{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>
    <!--Treeview-->
    <script src="{{ url_for('static', filename='plugins/bootstrap-treeview/js/bootstrap-treeview.min.js') }}"></script>

    <script type="text/javascript">

        $(function () {
            (function () {
                $.ajax({
                    type: 'get',
                    url: '{{ url_for("perms.api-permission-menu") }}',
                    success: function (menus) {
                        bees.initRoleTree($(".role-tree"), {data: menus}, '{{ permission_ids }}'.split(','));
                    }
                });
            })();

            var $formEle = $('form[name="roleForm"]');
            bees.niceValidate($formEle);
            $formEle.on("valid.form", function () {
                setAjaxCSRFToken();

                // 获取所有选择的权限ID
                var checkedPrivileges = $(".role-tree").treeview('getChecked');
                var checkedPrivilegeIds = [];
                $.each(checkedPrivileges, function (index, value) {
                    checkedPrivilegeIds.push(value.id);
                });

                $.ajax({
                    type: 'post',
                    url: '{{ url_for("perms.role-update", pk=default_pk) }}'.replace(bees.defaultPK, '{{ id }}'),
                    data : {
                        'name': $('input[name="name"]').val(),
                        'describe': $('textarea[name="describe"]').val(),
                        'permissions': checkedPrivilegeIds.join(',')
                    },
                    success: function (result) {
                        if (result.code != 200) {
                            $formEle.find('*[name='+ result.data.field +']').focus().addClass('has-error');
                            bees.tipError({
                                message: result.data.message
                            });
                        }else {
                            bees.tipInfo({
                                message: '修改成功'
                            });
                            window.location.href = '{{ url_for("perms.role-list") }}'
                        }
                    }
                });
            })
        })

    </script>

{% endblock %}
